<?php

namespace app\common\library;
use think\Facade\Db;
use Intervention\Image\ImageManager;
use Intervention\Image\Drivers\Gd\Driver;
use Intervention\Image\Encoders\AutoEncoder;
use Intervention\Image\Typography\FontFactory;
use app\common\library\QrcodeCreate;
use app\weapp\model\Activity as ActivityModel;
class ImageBind {

    public static function bulidImage($id=null)
    {
        $info = ActivityModel::find($id);
        if(!$info) return false;
        $Manager = new ImageManager(new Driver());
        $image_path = public_path().'static'.DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR;
        $fontfile =$image_path.'sy.otf';
        

        /**活动缩略图*/
        $thumb_path = public_path().$info['image'];
        $thumbImg = $Manager->read($thumb_path)->scale(680);
    
        /** 背景后图与前图*/
        $BgImg = $Manager->read($image_path.'abg.jpg');
        $wgImg = $Manager->read($image_path.'bbg.png');
        $qrcodeString = QrcodeCreate::create(request()->domain().''.$id,210,7);
        $qrcodeImage = $Manager->read($qrcodeString);
        $BgImg->place($thumbImg,'top-left',35,70)->place($wgImg,'top-left',0,0)->text($info['title'], 70, 580, function (FontFactory $font)use ($fontfile) {
            $font->filename($fontfile);
            $font->size(32);
            $font->color('000');
            $font->align('left');
            $font->valign('bottom');
            $font->lineHeight(2.0);
            $font->wrap(608);
        })->text('时间：'.$info['start_time'], 70, 610, function (FontFactory $font)use ($fontfile) {
            $font->filename($fontfile);
            $font->size(24);
            $font->color('444');
            $font->align('left');
            $font->valign('top');
            $font->lineHeight(2.0);
            $font->wrap(608);
        })->text('地点：'.$info['city_text'].$info['address'], 70, 658, function (FontFactory $font)use ($fontfile) {
            $font->filename($fontfile);
            $font->size(24);
            $font->color('444');
            $font->align('left');
            $font->valign('top');
            $font->lineHeight(2.0);
            $font->wrap(608);
        })->place($qrcodeImage,'top-left',270,820)->text('长 按 识 别 二 维 码， 参 与 活 动', 375, 1060, function (FontFactory $font)use ($fontfile) {
            $font->filename($fontfile);
            $font->size(25);
            $font->color('111');
            $font->align('center');
            $font->valign('middle');
            $font->lineHeight(2.0);
            $font->wrap(750);
        });
        
        $BgImg->save($image_path.'special'.DIRECTORY_SEPARATOR.$id.'.jpg');
    }

    
}